package com.flag.liutao.modules.student.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.student.domain.Department;
import com.flag.liutao.modules.student.domain.Major;
import com.flag.liutao.modules.student.mapper.MajorMapper;
import com.flag.liutao.modules.student.param.MajorParam;
import com.flag.liutao.modules.student.service.IMajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.student.vo.MajorVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-06
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements IMajorService {
    @Autowired
    private MajorMapper majorMapper;

    @Override
    public Map<String, Object> getPage(int page, int limit, String keyword, Long deptId) {
        IPage<MajorVo> iPage = new Page<>(page,limit);
        IPage pageDB = majorMapper.getMajorPage(iPage,keyword,deptId);
        Map<String,Object> map = new HashMap<>();
        map.put("content",pageDB.getRecords());
        map.put("total",pageDB.getTotal());
        return map;
    }

    @Override
    public void addMajor(MajorParam param) {
        LambdaQueryWrapper<Major> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Major::getMajorName,param.getMajorName());
        Major major = majorMapper.selectOne(lqw);
        if (ObjectUtil.isNotNull(major)) {
            throw new MyException("专业名称："+major.getMajorName()+"已存在");
        }
        Major majorDB = new Major();
        BeanUtil.copyProperties(param,majorDB);
        majorMapper.insert(majorDB);
    }

    @Override
    public void editMajor(MajorParam param) {
        Major major1 = majorMapper.selectById(param.getId());
        if(ObjectUtil.isNull(major1)){
            throw new MyException("该专业不存在");
        }
        LambdaQueryWrapper<Major> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Major::getMajorName,param.getMajorName());
        Major major2 = majorMapper.selectOne(lqw);
        if (ObjectUtil.isNotNull(major2) && major2.getId()!=param.getId()) {
            throw new MyException("专业名称："+major2.getMajorName()+"已存在");
        }
        Major majorDB = new Major();
        BeanUtil.copyProperties(param,majorDB);
        majorMapper.updateById(majorDB);
    }

    @Override
    public void delMajor(Long id) {
        majorMapper.deleteById(id);
    }
}
